<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" >

<title>每日一题20201117（221. 最大正方形） | 小克的blog</title>

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="shortcut icon" href="https://woodywrx.gitee.io/blog/favicon.ico?v=1615823433634">
<link rel="stylesheet" href="https://woodywrx.gitee.io/blog/styles/main.css">



<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>



    <meta name="description" content="221. 最大正方形

动态规划
求的是最大面积，可以转换为求最大边长。

创建一个二维数组dp

dp是以i, j坐标为右下角的正方形的最大边长。

状态转移方程式：

matrix[i][j] == &quot;1&quot;的时候:
..."/>
    <meta name="keywords" content="leetcode"/>
</head>
<body>

<div id="app" class="main">

    <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://woodywrx.gitee.io/blog">
        <img src="https://woodywrx.gitee.io/blog/images/avatar.png?v=1615823433634" class="site-logo">
        <h1 class="site-title">小克的blog</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="https://woodywrx.gitee.io/blog" class="site-nav">
            首页
          </a>
        
      
        
          <a href="https://woodywrx.gitee.io/blog/tags" class="site-nav">
            标签
          </a>
        
      
        
          <a href="https://woodywrx.gitee.io/blog/post/about" class="site-nav">
            关于
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="social-container">
      
        
      
        
      
        
      
        
      
        
      
    </div>
    <div class="site-description">
      欢迎来到我的小窝~这里不仅有博客，也有日记。
    </div>
    <div class="site-footer">
      wuranxu's blog | <a class="rss" href="https://woodywrx.gitee.io/blog/atom.xml" target="_blank">RSS</a>
    </div>
  </div>
</div>


    <div class="main-container">
        <div class="content-container" data-aos="fade-up">
            <div class="post-detail">
                <h2 class="post-title">每日一题20201117（221. 最大正方形）</h2>
                <div class="post-date">2020-11-17 11:31:25</div>
                
                <div class="post-content" v-pre>
                    <h4 id="221-最大正方形"><a href="https://leetcode-cn.com/problems/maximal-square/">221. 最大正方形</a></h4>
<figure data-type="image" tabindex="1"><img src="https://tva1.sinaimg.cn/large/0081Kckwgy1gkrzqo43nvj30yy0oatb4.jpg" alt="image-20201117110606881" loading="lazy"></figure>
<h3 id="动态规划">动态规划</h3>
<pre><code>求的是最大面积，可以转换为求最大边长。

创建一个二维数组dp

dp是以i, j坐标为右下角的正方形的最大边长。

状态转移方程式：

matrix[i][j] == &quot;1&quot;的时候:
f(i, j) = min(f(i-1, j), f(i, j-1), f(i-1, j-1)) + 1 

matrix[i][j] == &quot;0&quot;的时候，以这个位置为边的长度肯定为0：

 f(i, j) = 0
</code></pre>
<pre><code class="language-Python">class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -&gt; int:
        # 判断数组是否为空
        if len(matrix) == 0 or len(matrix[0]) == 0:
            return 0
        dp = [[0 for _ in n] for n in matrix]
        # 定义最大边长
        max_len = 0
        for i in range(len(matrix)):
            for j in range(len(matrix[i])):
                # 如果i = 0 或者 j = 0 他们是靠边的，所以最多只能以他们本身为边
                if i == 0 or j == 0:
                    dp[i][j] = int(matrix[i][j])
                    if dp[i][j] &gt; max_len:
                        max_len = dp[i][j]
                    continue
                if matrix[i][j] == '0':
                    dp[i][j] = 0
                else:
                    # 找到3个之中最小的+1，因为已经确定matrix[i][j]不为'0'
                    dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
                    if dp[i][j] &gt; max_len:
                        max_len = dp[i][j]
        return max_len * max_len

</code></pre>
<hr>
<p><strong>需要注意的是，matrix里面的元素都是字符串不是int</strong></p>
<figure data-type="image" tabindex="2"><img src="https://tva1.sinaimg.cn/large/0081Kckwgy1gkrzpe79huj30ye0eita8.jpg" alt="image-20201117110450683" loading="lazy"></figure>

                </div>
                
                    <div class="tag-container">
                        
                            <a href="https://woodywrx.gitee.io/blog/vp-bY7-nD/" class="tag">
                                leetcode
                            </a>
                        
                    </div>
                

                
                    <div class="next-post">
                        <div class="next">下一篇</div>
                        <a href="https://woodywrx.gitee.io/blog/post/mei-ri-yi-ti-2020111555-tiao-yue-you-xi/">
                            <h3 class="post-title">
                                每日一题20201115（55. 跳跃游戏）
                            </h3>
                        </a>
                    </div>
                
                
                    <span id="/blog/post/mei-ri-yi-ti-20201117221-zui-da-zheng-fang-xing/"
                          class="leancloud_visitors" data-flag-title="每日一题20201117（221. 最大正方形）">
                <em class="post-meta-item-text">阅读量 </em>
                <i class="leancloud-visitors-count">0</i>
            </span>
                
                
                    

	<div id="vcomments" style="width: 100%;max-width:1000%;padding:2.5%"></div>



                

            </div>

        </div>
    </div>
</div>

<script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script type="application/javascript">

AOS.init();

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>






<script src='https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'fT8wvEVNtx1cOcCQEs7rVwnV-gzGzoHsz',
        appKey: 'xV6aDHKSkLfP7u0cBRIzpmcy',
        avatar: '',
        pageSize: 5,
        recordIp: true,
        placeholder: 'Just Go Go',
        visitor: true,
    });
</script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</body>
</html>
